package cn.kn.triage.sys.service.impl;

import cn.kn.triage.common.exception.CustomException;
import cn.kn.triage.common.model.PageBean;
import cn.kn.triage.common.model.PageResult;
import cn.kn.triage.common.model.R;
import cn.kn.triage.common.model.RCode;
import cn.kn.triage.common.servic.BaseService;
import cn.kn.triage.common.utils.BeanCopy;
import cn.kn.triage.sys.dao.UserRoleDao;
import cn.kn.triage.sys.mapper.RoleMapper;
import cn.kn.triage.sys.model.Role;
import cn.kn.triage.sys.model.RoleExample;
import cn.kn.triage.sys.service.RoleService;
import cn.kn.triage.sys.vo.IRoleVo;
import cn.kn.triage.sys.vo.ORoleVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author yinzn
 * @create 2020-10-16 14:31
 */
@Service
public class RoleServiceImpl extends BaseService implements RoleService {
    private final RoleMapper roleMapper;

    public RoleServiceImpl(RoleMapper roleMapper, UserRoleDao userRoleDao) {
        this.roleMapper = roleMapper;
    }


    @Override
    public R add(Role role) {
        isExist(role);
        roleMapper.insertSelective(role);
        return R.ok();
    }

    private void isExist(Role role) {
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria()
                .andCnameEqualTo(role.getCname())
                .andIsdelEqualTo(false);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        if (!CollectionUtils.isEmpty(roles)){
            throw new CustomException(RCode.Role_NAME_REPEAT);
        }
    }

    @Override
    public R del(Long roleId) {
        /*一般情况是假啥删除*/
        Role role = new Role();
        role.setId(roleId);
        role.setIsdel(true);
        roleMapper.updateByPrimaryKeySelective(role);
        return R.ok();
    }

    @Override
    public R get() {
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andIsdelEqualTo(false);
        List<Role> roles = roleMapper.selectByExample(roleExample);
      /*
       java8.0 以前的写法
       List<ORoleVo> oRoleVos = new ArrayList<>();
        for (Role role:roles){
            ORoleVo oRoleVo =new ORoleVo();
            BeanUtils.copyProperties(role,oRoleVo);
            oRoleVos.add(oRoleVo);
        }*/
       /* java8.0之后的写法
       * roles.stream()管道流  按流来操作的
       * */
       /* List<ORoleVo> oRoleVos =roles.stream().map(role->{
            ORoleVo oRoleVo =new ORoleVo();
            BeanUtils.copyProperties(role,oRoleVo);
            return oRoleVo;
        }).collect(Collectors.toList());*/
        /*
        * ORoleVo::new  引用ORoleVo构造函数
        * */

        return R.ok(BeanCopy.beanCopy(roles,ORoleVo::new));
    }

    @Override
    public R update(Long roleId, IRoleVo iRoleVo) {
        Role role = BeanCopy.beanCopy(iRoleVo, Role::new);
        role.setId(roleId);
        roleMapper.updateByPrimaryKeySelective(role);
        return R.ok();
    }

    @Override
    public R list(PageBean pageBean, String roleName) {
       // List<Role> r = new ArrayList();
        RoleExample roleExample =new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria()
                .andIsdelEqualTo(false);
        if (!StringUtils.isEmpty(roleName)){
            criteria.andCnameLike("%"+roleName+"%");
        }
        PageHelper.startPage(pageBean.getCurPage(),pageBean.getPageSize());

        List<Role> roles = roleMapper.selectByExample(roleExample);

       /* for (Role role : roleMapper.selectByExample(roleExample)) {
            r.add(role);
            System.out.println(role);
        }*/
        PageInfo<Role> pageInfo =new PageInfo(roles);

        return R.ok(new PageResult<>(pageInfo.getTotal(),BeanCopy.beanCopy(pageInfo.getList(),ORoleVo::new)));
    }

    @Override
    public R all() {
        RoleExample roleExample =new RoleExample();
        roleExample.createCriteria().andIsdelEqualTo(false);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        return R.ok(BeanCopy.beanCopy(roles,ORoleVo::new));
    }


}
